/** 
 *  @file       TFT_LCD.c
 *  @brief      240*432 TFT液晶驱动程序
 *  @author      
 *  @version    
 *  @date       2015-10 
 */
 
#include "TFT_LCD.h"
#include "R61509.h"
#include "charcode.h"

/**< 如果要显示图片就添加这个头文件 */
#ifdef PICTURE_SHOW

#include "picture.h"

#endif

/**
 *  @brief  TFT初始化配置 设定初始化背景颜色
 *  @param  None
 *  @return None
 */
void TFT_config(unsigned int background_color)
{
    TFT_init();
    TFT_clear_screen(background_color);      
}

/**
 *  @brief  给一块3*3像素涂上颜色
 *  @param  [in] x
 *  @param  [in] y
 *  @param  [in] color
 *  @return None
 */
void TFT_dot(unsigned int x, unsigned int y, unsigned int color)
{  
	unsigned char i;

	TFT_set_window(x - 1, y - 1, x + 1, y + 1);  /**< 单个像素 */

	for(i = 0; i < 9; i++)
	{
		TFT_write_data(color);
	}
}


/**
 *  @brief  给一个区域涂上颜色
 *  @param  [in] start_x
 *  @param  [in] start_y
 *  @param  [in] end_x
 *  @param  [in] end_y
 *  @param  [in] color
 *  @return None
 */
//void GUI_Box(unsigned int start_x, unsigned int start_y, unsigned char end_x, unsigned int end_y, unsigned int color)
//{ 
//	unsigned int temp;   

//	TFT_set_window(start_x, start_y, end_x, end_y); 
//  start_x = end_x - start_x + 1;
//	start_y = end_y - start_y + 1;
//	while (start_x--)
//	{
//	 	temp = start_y;
//		while (temp--)
//	 	{	
//			TFT_write_data(color);
//		}
//	}
//}
  
/**
*  @brief  画一条直线
*  @param  [in] start_x
*  @param  [in] start_y
*  @param  [in] end_x
*  @param  [in] end_y
*  @param  [in] color	直线的颜色
*  @return None
*/
void TFT_draw_line(unsigned int start_x, unsigned int start_y, unsigned char end_x, unsigned int end_y, unsigned int color)
{
	unsigned int t;  
    
	int xerr = 0, yerr = 0, delta_x, delta_y, distance;  
	int incx, incy;  
	unsigned int row, col;  
    
	delta_x = end_x - start_x;  /**< 计算坐标增量  */
	delta_y = end_y - start_y;  
    
	col = start_x;  
	row = start_y;  
    
	if (delta_x > 0)
	{
		incx=1;				/**< 设置单步方向 */
	} 	  
	else    
	{  
	    if (delta_x == 0)
		{
			incx = 0;		/**< 垂直线  */
		} 		 
	    else 
        {
            incx = -1;
            delta_x = -delta_x;
        }  
	}  
	if (delta_y > 0)
	{
		incy = 1;
	}	  
	else  
	{  
	    if (delta_y == 0)
		{
			incy = 0;		/**< 水平线 */  
		} 
	    else 
		{
			incy = -1;
			delta_y = -delta_y;
		}  
	}  
    
	if (delta_x > delta_y)
	{ 
		distance = delta_x;	/**< 选取基本增量坐标轴  */
	}
	else
	{
		distance = delta_y; 
	} 	
    
	for (t = 0; t <= distance + 1; t++)  
	{                                     /**< 画线输出  */
	    TFT_dot(col, row, color);
	    xerr += delta_x;  
	    yerr += delta_y;  
	  	if(xerr > distance)  
	    {  
	        xerr -= distance;  
	        col += incx;  
	    }  
	    if(yerr > distance)  
	    {  
	        yerr -= distance;  
	        row += incy;  
	    }  
	}  
}

#ifdef CHAR_32x29_CHN_SHOW

/**
 *  @brief  写二号楷体汉字
 *  @param  [in] x
 *  @param  [in] y
 *  @param  [in] chn 要显示的汉字
 *  @param  [in] word_color
 *  @param  [in] background_color
 *  @return None
 */
void TFT_write_char_32x29_CHN(unsigned int x, unsigned int y, unsigned char *chn, unsigned int word_color, unsigned int background_color)	 
{  
	unsigned char i, j;
    unsigned char word_num;
	unsigned int  color;
    
	while (*chn != '\0')
	{
		TFT_set_window(x, y, x+31, y+28);
		for (word_num = 0; word_num < 20; word_num++)
		{	/** word_num扫描字库的字数 */
			if ((CnChar32x29[word_num].Index[0] == *chn)
			     &&(CnChar32x29[word_num].Index[1] == *(chn+1)))
			{
				for(i = 0; i < 116; i++) 
				{	/** MSK的位数 */
					color = CnChar32x29[word_num].Msk[i];
					for(j = 0;j < 8;j++) 
					{
						if((color&0x80) == 0x80)
						{
							TFT_write_data(word_color);
						} 						
						else
						{
							TFT_write_data(background_color);
						} 
						color <<= 1;
					}//for(j=0;j<8;j++)结束
				}    
			}
		} //for (word_num = 0; word_num < 20; word_num++)结束 	
		chn += 2;
		x += 32;
	}
}
#endif

#ifdef CHAR_19x24_CHN_SHOW

/**
 *  @brief  写四号字
 *  @param  [in] x
 *  @param  [in] y
 *  @param  [in] chn 要显示的汉字
 *  @param  [in] word_color
 *  @param  [in] background_color
 *  @return None
 */
void TFT_write_char_19x24_CHN(unsigned int x, unsigned int y, unsigned char *chn, unsigned int word_color, unsigned int background_color)
{  
	unsigned char i, j;
    unsigned char word_num;
	unsigned int color;
    
	while (*chn != '\0')
	{
		TFT_set_window(x, y, x + 23, y + 18);
        
		for (word_num = 0; word_num < 20; word_num++)
		{	
			if ((CnChar19x24[word_num].Index[0] == *chn)
			     &&(CnChar19x24[word_num].Index[1] == *(chn+1)))
			{
				for(i = 0; i < 57; i++) 
				{	
					color = CnChar19x24[word_num].Msk[i];
					for(j = 0; j < 8; j++) 
					{
						if((color&0x80) == 0x80)
						{
							TFT_write_data(word_color);
						} 						
						else
						{
							TFT_write_data(background_color);
						} 
						color <<= 1;
					}//for(j=0;j<8;j++)结束
				}    
			}
		} //for (word_num = 0; word_num < 20; word_num++)结束 	
		chn += 2;
		x += 24;
	}
}

#endif 

#ifdef CHAR_16x24_ASCII_SHOW

/**
 *  @brief  写四号字
 *  @param  [in] x
 *  @param  [in] y
 *  @param  [in] *p 要显示的ASCII字符
 *  @param  [in] word_color
 *  @param  [in] background_color
 *  @return None
 */
void TFT_write_16x24_ASCII(unsigned int x, unsigned int y, unsigned char *p, unsigned int word_color, unsigned int background_color)
{
	unsigned char j;
    unsigned char word_byte,word_num;
	unsigned int  color;
    
	while(*p != '\0')
	{
		word_num = *p - 32;
		TFT_set_window(x, y, x + 15, y + 23);
        
		for (word_byte = 0; word_byte < 48; word_byte++)
		{
			color = ASCII_16x24[word_num][word_byte];
			for (j = 0; j < 8; j++) 
			{
				if ((color&0x80) == 0x80)
				{
					TFT_write_data(word_color);
				} 						
				else
				{
					TFT_write_data(background_color);
				} 	
				color <<= 1;
			}
		}
		p++;
		x += 16;
	}
}

#endif  

#ifdef PICTURE_SHOW
/**
*  @brief  TFT显示图片 pic数组取自头文件pitcture.h
*  @param  [in] x	图片起始坐标
*  @param  [in] y
*  @param  [in] wide 图片宽度
*  @param  [in] high 图片高度
*  @return None
*/
void TFT_show_picture(unsigned int x, unsigned int y, unsigned char wide, unsigned int high)
{
	unsigned int temp = 0;
    unsigned int tmp = 0;
    unsigned int num = 0;
    
	TFT_set_window(x, y, x + wide - 1, y + high - 1);
	num = wide * high * 2;
	do
	{  
		temp = pic[tmp + 1];
		temp = temp << 8;
		temp = temp | pic[tmp];
		TFT_write_data(temp);		/**< 逐点显示 */
		tmp += 2;
	}
	while(tmp < num);	
}

#endif
